home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / tcl / tcl70b2.lha / tcl7.0b2 / doc / upvar.n < prev    next >
Text File  |  1993-06-16  |  3KB  |  84 lines

  1. '\"
  2. '\" Copyright (c) 1993 The Regents of the University of California.
  3. '\" All rights reserved.
  4. '\"
  5. '\" Permission is hereby granted, without written agreement and without
  6. '\" license or royalty fees, to use, copy, modify, and distribute this
  7. '\" documentation for any purpose, provided that the above copyright
  8. '\" notice and the following two paragraphs appear in all copies.
  9. '\"
  10. '\" IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
  11. '\" FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  12. '\" ARISING OUT OF THE USE OF THIS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13. '\" CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. '\"
  15. '\" THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16. '\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17. '\" AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18. '\" ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19. '\" PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20. '\" 
  21. '\" $Header: /user6/ouster/tcl/man/RCS/upvar.n,v 1.3 93/06/16 16:41:13 ouster Exp $ SPRITE (Berkeley)
  22. '\" 
  23. .so man.macros
  24. .HS upvar tcl
  25. .BS
  26. '\" Note:  do not modify the .SH NAME line immediately below!
  27. .SH NAME
  28. upvar \- Create link to variable in a different stack frame
  29. .SH SYNOPSIS
  30. \fBupvar \fR?\fIlevel\fR? \fIotherVar myVar \fR?\fIotherVar myVar \fR...?
  31. .BE
  32.  
  33. .SH DESCRIPTION
  34. .PP
  35. This command arranges for one or more local variables in the current
  36. procedure to refer to variables in an enclosing procedure call or
  37. to global variables.
  38. \fILevel\fR may have any of the forms permitted for the \fBuplevel\fR
  39. command, and may be omitted if the first letter of the first \fIotherVar\fR
  40. isn't \fB#\fR or a digit (it defaults to \fB1\fR).
  41. For each \fIotherVar\fR argument, \fBupvar\fR makes the variable
  42. by that name in the procedure frame given by \fIlevel\fR (or at
  43. global level, if \fIlevel\fR is \fB#0\fR) accessible
  44. in the current procedure by the name given in the corresponding
  45. \fImyVar\fR argument.
  46. The variable named by \fIotherVar\fR need not exist at the time of the
  47. call;  it will be created the first time \fImyVar\fR is referenced, just like
  48. an ordinary variable.
  49. \fBUpvar\fR may only be invoked from within procedures.
  50. .VS
  51. \fIMyVar\fR may not refer to an element of an array, but \fIotherVar\fR
  52. may refer to an array element.
  53. .VE
  54. \fBUpvar\fR returns an empty string.
  55. .PP
  56. The \fBupvar\fR command simplifies the implementation of call-by-name
  57. procedure calling and also makes it easier to build new control constructs
  58. as Tcl procedures.
  59. For example, consider the following procedure:
  60. .DS
  61. .ta 1c 2c 3c
  62. \fBproc add2 name {
  63.     upvar $name x
  64.     set x [expr $x+2]
  65. }
  66. .DE
  67. \fBAdd2\fR is invoked with an argument giving the name of a variable,
  68. and it adds two to the value of that variable.
  69. Although \fBadd2\fR could have been implemented using \fBuplevel\fR
  70. instead of \fBupvar\fR, \fBupvar\fR makes it simpler for \fBadd2\fR
  71. to access the variable in the caller's procedure frame.
  72. .PP
  73. .VS
  74. If an upvar variable is unset (e.g. \fBx\fR in \fBadd2\fR above), the
  75. \fBunset\fR operation affects the variable it is linked to, not the
  76. upvar variable.  There is no way to unset an upvar variable except
  77. by exiting the procedure in which it is defined.  However, it is
  78. possible to retarget an upvar variable by executing another \fBupvar\fR
  79. command.
  80. .VE
  81.  
  82. .SH KEYWORDS
  83. context, frame, global, level, procedure, variable
  84.